package com.babel.util.tools;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class JsonUtilsForHashDataTransaction {

    private static ObjectMapper mapper = new ObjectMapper();

    /**
     * <code>toObject</code>
     * @description: json转换成实体
     * @since 2017-8-3     陈辉
     */
    public static <T> T toObject(String json, Class<T> clazz) {
        T object = null;
        try {
            object = mapper.readValue(json, clazz);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return object;
    }

    /**
     * <code>toList</code>
     * @description: json转换list
     * @since 2017-8-3     陈辉
     */
    public static <T> List<T> toList(String listJson, Class<T[]> clazz) {
        List<T> result = null;
        try {
            T[] arrays = mapper.readValue(listJson, clazz);
            result = Arrays.asList(arrays);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return result;
    }

    /**
     * <code>linkedHashMapToJson</code>
     * @description: linkedHashMap对象转换成标准json
     * @since 2017-8-3     陈辉
     */
    public static String linkedHashMapToJson(Object linkedHashMap) {

        String listStr = linkedHashMap.toString();
        String optStr = listStr.trim();
        String[] strSet = optStr.split("=");
        String leftBracket = "{";
        String rightBracket = "}";
        StringBuilder result = new StringBuilder();

        for (int i = 0; i < strSet.length; i++) {
            boolean withLeftBracket = strSet[i].contains(leftBracket);
            boolean withRightBracket = strSet[i].contains(rightBracket);

            if (withLeftBracket && !withRightBracket) {
                String[] left = strSet[i].split("[{]");
                result.append("{").append("\"").append(left[1]).append("\"").append(":");
            } else if (!withLeftBracket && !withRightBracket) {
                String[] meddle = strSet[i].split(",");
                result.append("\"").append(meddle[0].trim()).append("\"").append(",").append("\"").append(meddle[1].trim()).append("\"").append(":");
            } else if (!withLeftBracket && withRightBracket) {
                String[] right = strSet[i].split("}");
                result.append("\"").append(right[0]).append("\"").append("}");
            }
        }

        return result.toString();
    }
    /**
     * <code>linkedHashMapToJson</code>
     * @description: linkedHashMapList对象转换成标准jsonList
     * @since 2017-8-3     陈辉
     */
    public static String linkedHashMapListToJson(Object linkedHashMapList) {
        String listStr = linkedHashMapList.toString();
        String optStr = listStr.trim();
        String[] strSet = optStr.split("=");
        String leftBracket = "{";
        String rightBracket = "}";
        StringBuilder result = new StringBuilder();

        for (int i = 0; i < strSet.length; i++) {
            boolean withLeftBracket = strSet[i].contains(leftBracket);
            boolean withRightBracket = strSet[i].contains(rightBracket);

            if (withLeftBracket && !withRightBracket) {
                String[] left = strSet[i].split("[{]");
                result.append(left[0]).append("{").append("\"").append(left[1]).append("\"").append(":");
            } else if (!withLeftBracket && !withRightBracket) {
                String[] meddle = strSet[i].split(",");
                result.append("\"").append(meddle[0].trim()).append("\"").append(",").append("\"").append(meddle[1].trim()).append("\"").append(":");
            } else if (withLeftBracket && withRightBracket) {
                String[] meddle = strSet[i].split("}, \\{");
                result.append("\"").append(meddle[0]).append("\"").append("},{").append("\"").append(meddle[1]).append("\"").append(":");
            } else if (!withLeftBracket && withRightBracket) {
                String[] right = strSet[i].split("}");
                result.append("\"").append(right[0]).append("\"").append("}").append(right[1]);
            }
        }

        return result.toString();
    }

}  